<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../common/EasyMysql.php");
require_once(dirname(__FILE__) . "/../config/ConfigDB.php");

/**
 * Description of CouponCom
 *
 * @author Administrator
 */
class CouponCom {

    //put your code here
    function __construct() {
        $dbconfig = $GLOBALS["config"] ['database'];
        $mysql = new EasyMysql($dbconfig['hostname'], $dbconfig ['port'], $dbconfig ['username'], $dbconfig ['password'], $dbconfig ['db'], $dbconfig ['charset']);
        $this->mysql = $mysql;
    }

    function getNumber($type) {
        try {
            $sql = "select count(1) from coupon where type={$type}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            return false;
        }
        return 0;
    }

    function getAllCoupon($type, $minlimit, $count) {
        try {
            $sql = "select * from coupon where type={$type} order by id desc limit {$minlimit},{$count}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return null;
    }
    
    function deleteCoupon($id){
        try{
            $sql = "delete from coupon where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    
    function addCoupon($type,$createtime,$finaltime,$code,$fullfee,$cut,$totalfee){
        try{
            $sql = "insert into coupon(type,code,create_time,final_time,full_fee,cut,total_fee) values({$type},'{$code}','{$createtime}','{$finaltime}','{$fullfee}','{$cut}',{$totalfee});SELECT @@Identity";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0]['@@Identity'];
            }
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    function getCoupon($id){
        try{
            $sql = "select * from coupon where id={$id}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    function updatestatus($status,$id){
        try{
            $sql = "update coupon set status={$status} where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
}
